﻿using System;

namespace PageAdmin.Utils
{

    /// <summary>
    /// 验证码
    /// </summary>
    public class VerificationCodeHelper
    {
        /// <summary>
        /// 检测验证码是否正确
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public static TipsInfo Validate(string code)
        {
            string cookieKey = "_VerificationCodeToken";
            TipsInfo tipsInfo = new TipsInfo()
            {
                Msg = "通过验证"
            };
            if (code == null)
            {
                tipsInfo.State = -1;
                tipsInfo.Msg = "验证码不能为null";
                return tipsInfo;
            }
            string cookieValidateCode = CookieHelper.Get(cookieKey);
            //检测验证码是否正确
            if (cookieValidateCode == null)
            {
                tipsInfo.State = -1;
                tipsInfo.Msg = "无效的验证码";
            }
            else
            {
                string desValidateCode = EncryptHelper.DesDecrypt(cookieValidateCode, SystemConfig.Get().SecretKey);//cookie解密
                if (desValidateCode == null)
                {
                    tipsInfo.State = -1;
                    tipsInfo.Msg = "验证码解密失败";
                }
                else if (!code.Equals(desValidateCode, StringComparison.CurrentCultureIgnoreCase))
                {
                    tipsInfo.State = 0;
                    tipsInfo.Msg = "验证码错误";
                }
            }
            return tipsInfo;
        }

    }
}